/**
 * 
 */
var registPassId=false;
var registPassPwd=false;
var registPassSurePwd=false;
var registPassEmail=false;
var registPassPhone=false;
function regist() {
	if(registPassId==true&&registPassPwd==true&&registPassSurePwd==true&&registPassEmail==true&&registPassPhone==true){
		return true;
	}else{
		return false;
	}
}
var xmlhttp;
var callbackUser;
function initAjax() {//初始化ajax
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}
function idChang() {//注册用户名检查
	var id=document.getElementById("userId").value;
	if(id==null||id==""){
		registPassId=false;
		document.getElementById("idError").innerHTML="*用户名不能为空";
	}
	else if(id.length<4||id.length>16){
		registPassId=false;
		document.getElementById("idError").innerHTML="*用户名长度不够(4~16位字符)";
	}
	else {
		initAjax();
		xmlhttp.open("post","../UserServlet?opt=id",true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
		        //HTTP协议状态为成功  
		        if (xmlhttp.status == 200) {
		        	var data=eval(xmlhttp.responseText);
					if(data[0].userId=="pass"){
						registPassId=true;
		                document.getElementById("idError").innerHTML = "可以注册";
					}else{
		                registPassId=false;
		                document.getElementById("idError").innerHTML = "用户已注册";
					}
		        }
			}
		};
		xmlhttp.send("name="+id);
	}

}
function pwdChang() {//注册密码检查
	var pwd=document.getElementById("userPassword").value;
	if(pwd==null||pwd=="")
		{
		registPassPwd=false;
		document.getElementById("pwdError").innerHTML="*密码不能为空";
		}
	else if(pwd.length<6||pwd.length>18){
		registPassPwd=false;
		document.getElementById("pwdError").innerHTML="*密码长度不够(6~18位字符)";
	}else {
		registPassPwd=true;
		document.getElementById("pwdError").innerHTML="*";
		}
}
function sureChang() {//确认密码检查
	var sure=document.getElementById("surePassword").value;
	var pwd=document.getElementById("userPassword").value;
	if(sure!=pwd){
		registPassSurePwd=false;
		document.getElementById("sureError").innerHTML="*密码不一致";
	}else {
		registPassSurePwd=true;
		document.getElementById("sureError").innerHTML="*";
	}
}
function emailChange() {//注册邮箱检查
	var email=document.getElementById("userEmail").value;
	var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
	if(email==null||email==""){
		registPassEmail=false;
		document.getElementById("emailError").innerHTML="*邮箱不能为空";
	}
	else{
		if(!re.test(email)){
			registPassEmail=false;
			document.getElementById("emailError").innerHTML="*邮箱格式不正确(如:123@qq.com)";
		}else {
			initAjax();
			xmlhttp.open("post","../UserServlet?opt=email",true);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
			        //HTTP协议状态为成功  
			        if (xmlhttp.status == 200) {
			        	var data=eval(xmlhttp.responseText);
						if(data[0].userEmail=="pass"){
							registPassEmail=true;
			                document.getElementById("emailError").innerHTML = "可以注册";
						}else{
							registPassEmail=false;
			                document.getElementById("emailError").innerHTML = "邮箱已注册";
						}
			        }
				}
			};
			xmlhttp.send("name="+email);
		}
	}
}
function phoneChange() {//注册电话检查
	var phone=document.getElementById("userPhone").value;
	var re=/^0?1[3|4|5|8][0-9]\d{8}$/;
	if(phone==null||phone==""){
		registPassPhone=false;
		document.getElementById("phoneError").innerHTML="*手机号码不能为空";
	}else{
		if(!re.test(phone)){
			registPassPhone=false;
			document.getElementById("phoneError").innerHTML="*请输入正确号码(如：13528433245)";
		}else{
			initAjax();
			xmlhttp.open("post","../UserServlet?opt=phone",true);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
			        //HTTP协议状态为成功  
			        if (xmlhttp.status == 200) {
			        	var data=eval(xmlhttp.responseText);
						if(data[0].userPhone=="pass"){
							registPassPhone=true;
			                document.getElementById("phoneError").innerHTML = "可以注册";
						}else{
							registPassPhone=false;
			                document.getElementById("phoneError").innerHTML = "电话号已注册";
						}
			        }
				}
			};
			xmlhttp.send("name="+phone);
		}
	}
}
var loginPassName=false;
var loginPassPwd=false;
function byLogin(){//ajax用户登陆
	if(loginPassName==true&&loginPassPwd==true){
		var name=document.getElementById("loginName").value;
		var pwd=document.getElementById("loginPassword").value;
		var data="loginName="+name+"&loginPassword="+pwd;
		callbackUser="login";
		initAjax();
		xmlhttp.open("post","../UserServlet?opt=login",true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.onreadystatechange = function(){
			//Ajax引擎状态为成功  
		    if (xmlhttp.readyState == 4) {
		        //HTTP协议状态为成功  
		        if (xmlhttp.status == 200) {
		        	var data=eval(xmlhttp.responseText);
		        	if(data[0].login=="pass"){
		        		window.location.href="../index.jsp";
		        	}else{
		        		alert("账户或密码错误");
		        	}
		        }
		    }
		};
		xmlhttp.send(data);
	}
}
function loginNameChange(){//登陆检查用户名输入情况
	var name=document.getElementById("loginName").value;
	if(name=="")
	{
	loginPassName=false;
	document.getElementById("loginError").innerHTML="用户名不能为空";
	document.getElementById("loginName").value="邮箱/用户名/手机号";
	}
	else if(name.length<4){
		loginPassName=false;
		document.getElementById("loginError").innerHTML="账户长度不够";
	}else {
		loginPassName=true;
		document.getElementById("loginError").innerHTML="";
		loginPwdChang();
	}
}
function loginNameNull(){//登陆用户名文本框样式
	var name=document.getElementById("loginName").value;
	if(name=="邮箱/用户名/手机号"){
		loginPassName=false;
		document.getElementById("loginName").value="";
	}
}
function loginPwdChang() {//登陆密码文本框样式，密码检查
	var pwd=document.getElementById("loginPassword").value;
	if(pwd==null||pwd=="")
	{
	loginPassPwd=false;
	document.getElementById("loginError").innerHTML="密码不能为空";
	document.getElementById("loginPassword").type="text";
	document.getElementById("loginPassword").value="密码";
	}
	else if(pwd.length<6){
		loginPassPwd=false;
		document.getElementById("loginError").innerHTML="密码长度不够";
	}else {
		loginPassPwd=true;
		document.getElementById("loginError").innerHTML="";
		loginNameChange();
	}
}
function loginPwdNull(){//登陆密码文本框样式
	var pwd=document.getElementById("loginPassword").value;
	if(pwd=="密码"){
		loginPassPwd=false;
		document.getElementById("loginPassword").value="";
		document.getElementById("loginPassword").type="password";
	}
}
//账户注销
function logout(){
	var url;
	if(document.title=="HOME"){
		url="UserServlet?opt=logout";
	}else{
		url="../UserServlet?opt=logout";
	}
	xmlhttp.open("post",url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function(){
		//Ajax引擎状态为成功  
	    if (xmlhttp.readyState == 4) {
	        //HTTP协议状态为成功  
	        if (xmlhttp.status == 200) {
	        	var data=eval(xmlhttp.responseText);
	        	if(!data[0].login){
	        		alert("注销成功");
	        		 window.location.reload();
	        	}
	        }
	    }
	};
	xmlhttp.send(null);
}
function loginIsFalseOrTrue(){//用户是否登陆，获取用户信息
	initAjax();
	document.getElementById("txt-search").value=getCookie("search");
	if(document.title=="HOME"){
		document.getElementById("linkLogin").setAttribute("href","pages/login.jsp" );
		document.getElementById("linkRegist").setAttribute("href","pages/regist.jsp" );
		document.getElementById("linkIndex").setAttribute("href","#" );
		document.getElementById("linkOrders").setAttribute("href","pages/login.jsp" );
		xmlhttp.open("post","UserServlet?opt=loginIsFalseOrTrue",true);
	}else{
		xmlhttp.open("post","../UserServlet?opt=loginIsFalseOrTrue",true);
	}
	xmlhttp.onreadystatechange = callbackByIsLogin;
	xmlhttp.send(null);
}
function callbackByIsLogin() { //获取用户信息
    //alert(xmlHttp.readyState);  
    //Ajax引擎状态为成功  
    if (xmlhttp.readyState == 4) {
        //HTTP协议状态为成功  
        if (xmlhttp.status == 200) {
        	var data=eval(xmlhttp.responseText);
            if (data[0].login=="nologin") {
            	document.getElementById("linkLogin").text="登陆";
            	document.getElementById("linkRegist").text="注册";
            }else if(data[0].login=="islogin"){
            	document.getElementById("linkLogin").text=data[0].userName;
            	document.getElementById("linkRegist").text="注销";
            	if(document.title=="HOME"){
            		document.getElementById("linkOrders").setAttribute("href","pages/orders.jsp" );
            	}else{
            		document.getElementById("linkOrders").setAttribute("href","orders.jsp" );
            	}
            	document.getElementById("linkLogin").setAttribute("href","javascript:void(0)" );
            	document.getElementById("linkRegist").setAttribute("href","javascript:void(0)" );
            	document.getElementById("linkRegist").setAttribute("onclick","logout()" );
            }else { 
            	alert("无法连接服务器");
            } 
        }else { 
            alert("请求失败，错误码=" + xmlhttp.status); 
        } 
    } 
}
//=======================cookie===============================
//写cookies 

function setCookie(name,value) 
{ 
  var Days = 30; 
  var exp = new Date(); 
  exp.setTime(exp.getTime() + Days*24*60*60*1000); 
  document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
} 

//读取cookies 
function getCookie(name) 
{ 
  var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

  if(arr=document.cookie.match(reg))

      return unescape(arr[2]); 
  else 
      return null; 
} 